#include "Cards.h"

#include <QLayout>
#include <QDebug>
#include "Player.h"
#include <QApplication>

Cards::Cards()
{
    m_bonusCount = 2;
}

void Cards::bonus()
{
    QList <Card* > m_selected;

    Card* m1 = m_selected[0];
    Card* m2 = m_selected[1];
    Card* m3 = m_selected[2];

    bool ok = false;
    if(m1->type == m2->type && m2->type == m3->type)
        ok = true;
    if(m1->color == m2->color && m2->color == m3->color)
        ok = true;
    if(m1->type != m2->type && m2->type != m3->type && m1->type != m3->type)
        ok = true;

    if(!ok)
        return;

    m1->state->player()->addReinforcement(m_bonusCount);
    emit reinforcementsAdded();

    m_bonusCount *= 2;
    if(m_bonusCount > 2048)
        m_bonusCount = 2048;

    for(int i = 0; i < m_selected.size(); ++i)
    {
        m_cards.removeOne(m_selected[i]);
        delete m_selected[i];
    }
}

Cards::~Cards()
{
    for(size_t i = 0; i < (unsigned)m_cards.size(); ++i)
    {
        delete m_cards[i];
    }
}
bool Cards::playerGotCard(const State* state, const Player* ply) const
{
    for(int i = 0; i < m_cards.size(); ++i)
    {
        if(m_cards[i]->state == state && m_cards[i]->owner == ply)
            return true;
    }
    return false;
}

void Cards::addCard(State* state)
{
    m_cards.append(new Card(state, state->player()));
}

bool Cards::changeCardOwner(const State* state, Player* owner)
{
    for(int i = 0; i < m_cards.size(); ++i)
    {
        if(m_cards[i]->state == state)
        {
            m_cards[i]->owner = owner;
            return false;
        }
    }

    return true;
}
